package com.ruoyi.common.utils;

/**
 * 地址工具类
 *
 * @author chenchuan2
 * @date 2023-03-16 10:36:22
 */
public class LocationUtil {

    /**
     * 默认地球半径
     */
    private static double EARTH_RADIUS = 6371000;//赤道半径(单位m)

    /**
     * 转化为弧度(rad)
     */
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * @param l1 第一点
     * @param l2 第二点
     * @return 返回的距离，单位m
     */
    public static double getDistance(LocationDTO l1, LocationDTO l2) {
        double radLat1 = rad(l1.getLatitude());
        double radLat2 = rad(l2.getLatitude());
        double a = radLat1 - radLat2;
        double b = rad(l1.getLongitude()) - rad(l2.getLongitude());
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    }
}
